package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class ThreadDemo36 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int rs = 0;
                for (int i = 1; i < 1000; i++) {
                    rs += i;
                }
                return rs;
            }
        };//此处不需要引入额外的成员变量,直接借助这里的返回值即可

        FutureTask<Integer> futureTask = new FutureTask<>(callable);//这个任务是未来执行的,
        Thread t = new Thread(futureTask);//Thread没有提供构造函数来传入callable
        //可以引入一个FutureTask类,作为Thread和callable的粘合剂
        t.start();
        //接下来这里也不需要join,使用futureTask直接获取到结果
        System.out.println(futureTask.get());
    }
}
